﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Yonkly.Utility;

namespace Yonkly  
{
    public class GlobalApplication : System.Web.HttpApplication
    {
        public GlobalApplication()
        {
            this.BeginRequest += new EventHandler(GlobalApplication_BeginRequest);
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            //Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
            //      automatic support on IIS6 and IIS7 classic mode
            #region Twitter Routes
            routes.Add(new Route("twitter/GetImportStatus", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "twitter", action = "GetImportStatus", Id = "" })
            });

            routes.Add(new Route("twitter/import", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "twitter", action = "import", Id = "" })
            });

            routes.Add(new Route("twitter/beginimport", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "twitter", action = "beginimport", Id = "" })
            });

            #endregion
            #region Group Routes
            routes.Add(new Route("group/update", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "update", Id = "" })
            });
            routes.Add(new Route("group/destroy", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "destroy", Id = "" })
            });

            routes.Add(new Route("group/joinsubmit", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "joinsubmit", Id = "" })
            });
            routes.Add(new Route("group/leavesubmit", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "leavesubmit", Id = "" })
            });
            routes.Add(new Route("group/list", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "list", Id = "" })
            });
            routes.Add(new Route("group/reply", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "reply", Id = "" })
            });
            routes.Add(new Route("group/deletemessage", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "deletemessage", Id = "" })
            });
            routes.Add(new Route("group/postmessage", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "postmessage", Id = "" })
            });
            routes.Add(new Route("group/approve", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "approve", Id = "" })
            });
            routes.Add(new Route("group/reject", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "reject", Id = "" })
            });
            routes.Add(new Route("group/new", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "new", Id = "" })
            });
            routes.Add(new Route("group/create", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "create", Id = "" })
            });
            routes.Add(new Route("group/CheckAvailability", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "CheckAvailability", Id = "" })
            });
            routes.Add(new Route("group", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "list", Id = "" })
            });
            routes.Add(new Route("group/{id}/{action}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "group", action = "index", Id = "" })
            });
            #endregion
            #region User Routes


            routes.Add(
                new Route("User/Login", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "User",
                                                                    action = "Login"
                                                                })
                    }
                );

            routes.Add(
                new Route("User/Logout", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "User",
                                                                    action = "Logout"
                                                                })
                    }
                );

            routes.Add(
                new Route("User/Signup", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "User",
                                                                    action = "Signup"
                                                                })
                    }
                );

            routes.Add(
                new Route("User/SendPassword", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "User",
                                                                    action = "SendPassword"
                                                                })
                    }
                );

            routes.Add(
                new Route("User/Follow", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "User",
                                                                    action = "Follow"
                                                                })
                    }
                );

            routes.Add(
                new Route("User/Unfollow", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "User",
                                                                    action = "Unfollow"
                                                                })
                    }
                );
            routes.Add(
                new Route("User/Find", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "User",
                                                                    action = "Find"
                                                                })
                    }
                );

            #endregion

            #region Message Routes
            routes.Add(new Route("message/postpic", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "message", action = "postpic", Id = "" })
            });

            routes.Add(new Route("message/RefreshMessages", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "message", action = "RefreshMessages", Id = "" })
            });
            routes.Add(
                new Route(string.Format("Message/Create"), new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "Message",
                                                                    action = "Create"
                                                                })
                    }
                );
            routes.Add(
                new Route("Message/Conversation/{Id}", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "Message",
                                                                    action = "Conversation"
                                                                })
                    }
                );
            routes.Add(
                new Route("Message/Reply", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "Message",
                                                                    action = "Reply"
                                                                })
                    }
                );

            routes.Add(
                new Route("Message/Edit", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "Message",
                                                                    action = "Edit"
                                                                })
                    }
                );
            routes.Add(
                new Route("Message/Delete", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "Message",
                                                                    action = "Delete"
                                                                })
                    }
                );

            routes.Add(
               new Route("Message/Friends/{username}", new MvcRouteHandler())
               {
                   Defaults = new RouteValueDictionary(new
                   {
                       controller = "Message",
                       action = "FriendsMessages"
                   })
               }
               );

            routes.Add(
              new Route("Message/Replies/{username}", new MvcRouteHandler())
              {
                  Defaults = new RouteValueDictionary(new
                  {
                      controller = "Message",
                      action = "Replies"
                  })
              }
              );
            routes.Add(
             new Route("Message/{username}", new MvcRouteHandler())
             {
                 Defaults = new RouteValueDictionary(new
                 {
                     controller = "Message",
                     action = "Messages"
                 })
             }
             );

            #endregion

            routes.Add(
                new Route("friends" + "/invite", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new { Controller = "Friends", Action = "Invite" })
                    });

            routes.Add(
                new Route("friends" + "/{*username}", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new { Controller = "Friends", Action = "View" })
                    });

            routes.Add(
               new Route("twitterfriends" + "/invite" + "/{*twitterusername}", new MvcRouteHandler())
               {
                   Defaults = new RouteValueDictionary(new { Controller = "TwitterFriends", Action = "Invite" })
               });
            routes.Add(
            new Route("twitterfriends", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { Controller = "twitterfriends", Action = "Index" })
            });


            routes.Add(
                new Route("followers/{*username}", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new { Controller = "followers", Action = "View" })
                    });

            #region Home Routes

            routes.Add(new Route("home/{action}", new MvcRouteHandler())
                           {
                               Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
                           });

            #endregion

            #region Profile Routes

            routes.Add(
                new Route("profile/ChangePassword", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "profile",
                                                                    action = "ChangePassword"
                                                                })
                    }
                );

            routes.Add(
                new Route("profile/Update", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "profile",
                                                                    action = "update"
                                                                })
                    }
                );

            routes.Add(
               new Route("profile/UpdateTwitter", new MvcRouteHandler())
               {
                   Defaults = new RouteValueDictionary(new
                   {
                       controller = "profile",
                       action = "UpdateTwitter"
                   })
               }
               );

            routes.Add(
                new Route("profile/{*catchall}", new MvcRouteHandler())
                    {
                        Defaults = new RouteValueDictionary(new
                                                                {
                                                                    controller = "profile",
                                                                    action = "edit"
                                                                })
                    }
                );

            #endregion


            routes.Add(new Route("link/{*action}", new MvcRouteHandler())
                           {
                               Defaults = new RouteValueDictionary(new { controller = "Link", action = "View", id = "" }),
                           });


            routes.Add(new Route("timeline/{*action}", new MvcRouteHandler())
                           {
                               Defaults = new RouteValueDictionary(new { controller = "timeline", action = "View" }),
                           });


            //routes.MapRoute("Default","Default.aspx");
            //routes.Add(new Route("Default.aspx", new MvcRouteHandler())
            //               {
            //                   Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
            //               });


            routes.Add(new Route("Error.aspx", new MvcRouteHandler())
                           {
                               Defaults = new RouteValueDictionary(new { controller = "Home", action = "Error" }),
                           });

            routes.Add(
                new Route("{*username}", new MvcRouteHandler())
                    {
                        Constraints = new RouteValueDictionary(new { username = "^(?!content/).*" }),
                        Defaults = new RouteValueDictionary(new { Controller = "User", action = "View" })
                    });
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
            
        }

        void GlobalApplication_BeginRequest(object sender, EventArgs e)
        {
            //RestoreCookies();
            Utils.RestoreCookies(Context);
        }

   
    }
}